/*
 * Copyright 2020 edenrun.cn All right reserved. This software is the
 * confidential and proprietary information ofedenrun.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with edenrun.cn.
 */

package com.zoom.pluto.smf.model.dto;

import com.zoom.pluto.smf.model.constants.ResponseConstants;
import com.zoom.pluto.smf.model.enums.ErrorCode;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

import static com.zoom.pluto.smf.model.constants.ResponseConstants.FAIL_MSG;

/**
 * 统一消息返回对象
 *
 * @author za-zhubin002 Created in 21:10 2019/2/28
 */
@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Result <T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 通信交易标识
     */
    private Boolean returnFlag;

    /**
     * 处理返回码
     */
    private String resultCode;

    /**
     * 处理返回结果
     */
    private String resultMsg;

    /**
     * 处理返回主体
     */
    private T resultContent;

    /**
     * 请求处理失败
     *
     * @return Result
     */
    public static <T> Result<T> fail() {
        return fail(ErrorCode.SYSTEM_ERROR.getErrorCode(), FAIL_MSG, null);
    }

    /**
     * 请求处理失败
     *
     * @param errorMsg 错误信息
     * @return Result
     */
    public static <T> Result<T> fail(String errorMsg) {
        return fail(ErrorCode.SYSTEM_ERROR.getErrorCode(), errorMsg, null);
    }

    /**
     * 请求处理失败
     *
     * @param errorCode 错误码
     * @param errorMsg  错误信息
     * @return Result
     */
    public static <T> Result<T> fail(String errorCode, String errorMsg) {
        return fail(errorCode, errorMsg, null);
    }

    /**
     * 请求处理失败
     *
     * @param errorCode 错误码
     * @param errorMsg  错误信息
     * @param data      错误主体
     * @return Result
     */
    public static <T> Result<T> fail(String errorCode, String errorMsg, T data) {
        return new Result<>(ResponseConstants.FAIL, errorCode, errorMsg, data);
    }

    /**
     * 请求处理成功
     *
     * @return Result
     */
    public static <T> Result<T> success() {
        return success(ResponseConstants.SUCCESS_MSG, null);
    }

    /**
     * 请求处理成功
     *
     * @param data 请求响应主体
     */
    public static <T> Result<T> success(T data) {
        return success(ResponseConstants.SUCCESS_MSG, data);
    }

    /**
     * 判断响应是否成功
     */
    public static boolean ifSuccess(Result result) {
        return ResponseConstants.SUCCESS_CODE.equals(result.resultCode);
    }

    /**
     * 请求处理成功
     *
     * @param successMsg 成功返回信息
     * @param data       请求响应主体
     * @return Result
     */
    private static <T> Result<T> success(String successMsg, T data) {
        return new Result<>(ResponseConstants.SUCCESS, ResponseConstants.SUCCESS_CODE, successMsg, data);
    }

}
